import axios from '@/util/axios';
import { OSS } from '@/util/constant'
import { vue } from '@/index';


const actions = {
    async userLogin(ctx: any, loginInfo: any) {
        let res = await axios.post('/user/login', loginInfo)
        if (res.data && res.status != 200) {
            vue.$message(res.msg)
            return res;
        }

        let { role_id, role_name, username, token } = res.data,
            userInfo: UserInfo = { role_id, role_name, username };
        // ctx.commit('loginInit', { userInfo, menus });
        ctx.commit('loginInit', { userInfo });
        localStorage.setItem('userInfo', JSON.stringify(userInfo));
        localStorage.setItem('Authorization', token);
        // localStorage.setItem('menus', JSON.stringify(menus));
        return res;
    },
    async getMenus (ctx: any) {
        let res = await axios.get('/menu/user/menus')
        if (res.data && res.status != 200) {
            vue.$message(res.msg)
            return res;
        }
        let { menuDtos } = res.data;
        ctx.commit('setMenu', menuDtos)
        localStorage.setItem('menus', JSON.stringify(menuDtos));
        return res;
    },
    async getPolicy(store: any) {
        try {
            let res = await axios.get(OSS.URL)
            store.commit('setPolicy', res.data)
        } catch (e) {
            console.log(e)
        }
    },
    async getAllTips (store: any) {
        let res = await axios.get('AdminCommonController/getAdminPendingCount')
        store.commit('setAllTips', res.data)

    },
    async getMemberTips (store: any) {
        let res = await axios.get('AdminCommonController/getAdminMemberPendingCount')
        store.commit('setMemberTips', res.data)
    },
    async getBillTips (store: any) {
        let { timeStyle, billStyleId } = store.state.tipState
        let res = await axios.get(`AdminCommonController/getAdminBillPendingCount?timeStyle=${timeStyle}&billStyleId=${billStyleId}`)
        store.commit('setBillTips', res.data)
    },
    async getOrderTips (store: any) {
        let { role, orderBillStyleId } = store.state.tipState
        let res = await axios.get(`AdminCommonController/getAdminOrderPendingCount?role=${role}&billStyleId=${orderBillStyleId}`)
        store.commit('setOrderTips', res.data)
    },
    async getReservateBillTips (store: any) {
        let res = await axios.get('reservatebill/reservatebill/count')
        store.commit('setReservateBillTips', res.data)
    },
    async getCheckServer (store: any) {
        let res = await axios.get('user/2/users')
        store.commit('setCheckServer', res.data)
    }
}
export default actions